﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using System.Collections.Generic;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Config;


public partial class Theme_3_MyStowResource : PowerCreator.LMS.Web.Base.BasePage
{
    PowerCreator.LMS.Web.Course.Resource op = new Resource();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    //protected string HtmlMyStowResoure = "";
    private int pagesize = 12;
    PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();
    private PowerCreator.LMS.Web.User.LoginEntity login = new PowerCreator.LMS.Web.User.LoginEntity();
    private string url = "UserLogin.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        CheckUser();
        this.Title = config.GetLanagerValueByKey("theme_center_MyFamily", PowerCreator.LMS.Language.LanagetItemType.Page);
        if (!IsPostBack)
        {
            //string search = GetSearch();
            //HtmlMyStowResoure = BinderMyStowResoure(search, 1);
            BinderMyStowResoure(1);
            BinderPager();
        }
    }




    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }

    }

    protected override void InitPage()
    {
        //btn_Search.Text = config.GetLanagerValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_Lanager_Notity.Text = GetLanageValueByKey("lbl_Lanager_Notity", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private string GetSearch()
    {
        string search = " ResourceID in";
        search += "  (select FKResourceID from TUserStowResource where FKUserID=" + login.UserID + " )";

        return search;
    }

    private void BinderMyStowResoure(int pageIndex)
    {
        string search = " ResourceID in";
        search += "  (select FKResourceID from TUserStowResource where FKUserID=" + login.UserID + " )";

        //string search = "CourseIsCompulsory = 3 and CourseState = 1";
        string orderList = " ResourceID desc";
        DataTable dt = op.QueryResource(search, orderList, pagesize, pageIndex);

        dt.Columns.Add("ResourceImageUrl");
        dt.Columns.Add("OpenUrl"); 
        dt.Columns.Add("Head"); 
        dt.Columns.Add("Foot"); 
        dt.Columns.Add("NoData"); 

        int t = 0;
        if (!PowerCreator.LMS.SCR.Dog.CheckDog())
        {

            for (int i = dt.Rows.Count - 1; i > 1; i--)
            {
                dt.Rows[i].Delete();
                t = t + 1;
            }

        }

        int iResult = 3;
        for (int i = 0; i < dt.Rows.Count -t; i++)
        {

            dt.Rows[i]["OpenUrl"] = "OpenCourseware.aspx?ResourceID=" + dt.Rows[i]["ResourceID"].ToString() + "&CourseResourceID=0&CourseID=0";
            if (dt.Rows[i]["ResourceClass"].ToString() == "1" || dt.Rows[i]["ResourceClass"].ToString() == "2")  //课件资源
            {
                string CoursersResourceUrl = GetPath(dt.Rows[i]["ResourceUrl"].ToString(), int.Parse(dt.Rows[i]["ResourceClass"].ToString()));
                dt.Rows[i]["ResourceImageUrl"] = CoursersResourceUrl + GetPicName(dt.Rows[i]["ResourceEntry"].ToString(), int.Parse(dt.Rows[i]["ResourceClass"].ToString()));
            }
            else 
            {
                dt.Rows[i]["ResourceImageUrl"] = "images/gkkpic.jpg";
            }
            int k = Math.DivRem(i, 4, out iResult);
            if (iResult == 0)
                dt.Rows[i]["Head"] = "<tr>";
            if (iResult == 3)
                dt.Rows[i]["Foot"] = "</tr>";
            


        }
        for (int j = 3 - iResult; j > 0; j--)
        {

            dt.Rows[dt.Rows.Count-1]["NoData"] = " <td>";
            dt.Rows[dt.Rows.Count - 1]["NoData"] += "<div class=\"center2ct\">";
            dt.Rows[dt.Rows.Count - 1]["NoData"] += "</div>";
            dt.Rows[dt.Rows.Count - 1]["NoData"] += "</td>";
            if (iResult == 0)
                dt.Rows[dt.Rows.Count - 1]["NoData"] += "</tr>";


        }

        rpt_MyStowResource.DataSource = dt;
        rpt_MyStowResource.DataBind();
    }


    private void BinderPager()
    {
        string search = " ResourceID in";
        search += "  (select FKResourceID from TUserStowResource where FKUserID=" + login.UserID + " )";

        AspNetPager1.PageSize = pagesize;
        AspNetPager1.RecordCount = op.QueryResourceCount(search);
    }

    protected void rpt_MyStowResource_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "del")
        {
            UserStowResource op = new UserStowResource();
            int ResourceID = int.Parse(e.CommandArgument.ToString());
            op.DelUserStowResource(ResourceID, login.UserID);
            BinderMyStowResoure(1);
            BinderPager(); 
        }
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BinderMyStowResoure(AspNetPager1.CurrentPageIndex);
    }


    private string GetPicName(string fileName, int Resourceclass)
    {
        string rt = "images/gkkpic.jpg";
        if (Resourceclass == 1)
        {
            rt = "000.jpg";
        }
        else if (Resourceclass == 2)
        {
            string[] fileNames = fileName.Split('.');
            if (fileNames.Length == 2)
                rt = fileNames[0].ToString() + ".jpg";
        }
        return rt;
    }
    private string GetPath(string Path, int Resourceclass)
    {
        string rt = Path;
        if (Resourceclass != 1 && Resourceclass != 2)
        {
            rt = "";
        }
        return rt;
    }

 }
